package com.adcc.swingui.serialize;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * <p>Title: AbstractSeriObject</p>
 * <p>Description: 序列化对象基类 </p>
 * <p>Copyright: Copyright (c) 2010</p>
 * @author jeff.zhao
 * @version 1.0.0
 */
public abstract class AbstractSeriObject implements Serializable
{
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(AbstractSeriObject.class);

	private transient boolean propertyChange = false ;
	/**值改变事件监听器*/
	private transient List<ISerializeObjectSavedListener> listenerList = new ArrayList<ISerializeObjectSavedListener>() ;
	private transient boolean systemInvoke = false ;
	
	public boolean isSystemInvoke() 
	{
		return systemInvoke;
	}

	public void setSystemInvoke(boolean systemInvoke) 
	{
		this.systemInvoke = systemInvoke;
	}

	/**改变的属性set方法名*/
	private transient HashSet<String> changePropertyNames = new HashSet<String>() ;
	
	public HashSet<String> getChangePropertyNames() 
	{
		return changePropertyNames;
	}

	public void setChangePropertyNames(HashSet<String> changePropertyNames) 
	{
		this.changePropertyNames = changePropertyNames;
	}

	public boolean takePropertyChange() 
	{
		return this.propertyChange ;
	}

	public void setPropertyChange(boolean propertyChange) 
	{
		this.propertyChange = propertyChange ;
	}
	
	public boolean isPropertyChange() {
		return propertyChange;
	}

	/**
	 * 注册保存事件
	 * @param changeLis
	 */
	public void addSavedListener( ISerializeObjectSavedListener changeLis ) 
	{
		if(listenerList == null) {
			listenerList = new ArrayList<ISerializeObjectSavedListener>();
		}
		listenerList.add( changeLis ) ;
	}
	/**
	 * 注册唯一事件
	 * @param changeLis
	 */
	public void addOneSavedListener( ISerializeObjectSavedListener changeLis ) 
	{
		if(listenerList == null) {
			listenerList = new ArrayList<ISerializeObjectSavedListener>();
		}
		listenerList.clear();
		listenerList.add(changeLis) ;
	}
	
	void addPropertyChangeName( String name )
	{
		if( changePropertyNames == null )
		{
			changePropertyNames = new HashSet<String>() ;
		}
		
		changePropertyNames.add( name ) ;
	}
	
	public List<ISerializeObjectSavedListener> getListenerList() 
	{
		if( listenerList == null )
		{
			listenerList = new ArrayList<ISerializeObjectSavedListener>() ;	
		}
		
		return listenerList;
	}

	public void setListenerList( List<ISerializeObjectSavedListener > listenerList ) 
	{
		this.listenerList = listenerList ;
	}
	
	/**
	 * 序列化对象到文件
	 * @param userName 当前用户名
	 * @return
	 */
	public boolean save(String userName) {
		boolean reflag = false;
		try {
			SerializeObjectFactory.saveSerializeObject(this.getClass().getName(), userName);
			reflag = true;
		} catch (Exception ex) {
			logger.error("序列化保存失败:" + ex.getMessage());
		}
		return reflag;
	}
	
	/**
	 * 序列化对象到文件，默认default目录
	 * @param userName 当前用户名
	 * @return
	 */
	public boolean save() {
		return save(null);
	}
	
	public static AbstractSeriObject getInstence() {
		return null;
	}
	/**
	 * 恢复默认值
	 */
	public abstract void restoreDefault() ;
}
